package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.brspace.qualitycontrol.entity.Dept;
import com.brspace.qualitycontrol.entity.vo.DeptVo;
import com.brspace.qualitycontrol.mapper.DeptMapper;
import com.brspace.qualitycontrol.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.brspace.qualitycontrol.utils.EmptyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author Hugo
 * @since 2021-04-18
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    /**
     *注册部门
     *@author Hugo
     *@date 2021-04-18 15:20
     **/
    @Override
    public void saveInfor(Dept dept) {
        if (dept.getPid()==null){
            dept.setLevel(0);
            dept.setPid("0");
        }else {
            System.out.println(dept.getPid());
            dept.setLevel(baseMapper.selectList(new QueryWrapper<Dept>().eq("id",dept.getPid())).get(0).getLevel()+1);
        }
        if (EmptyUtil.isEmpty(dept.getName())) {
            throw  new SecurityException("请检查参数！");
        }
        if (baseMapper.selectCount(new QueryWrapper<Dept>().eq("pid",dept.getPid()).eq("name",dept.getName()))>1){
            throw new SecurityException("部门名请勿重复");
        }
        baseMapper.insert(dept);
    }

    /**
     *查询部门树
     *@author Hugo
     *@date 2021-04-19 14:26
     **/
    @Override
    public List<DeptVo> getTreeList() {
        List<Dept> depts = baseMapper.selectList(new QueryWrapper<Dept>().eq("pid", "0"));
        List<Dept> depts1 = baseMapper.selectList(new QueryWrapper<Dept>().ne("pid", "0"));
        List<DeptVo> listTree = new ArrayList<>();
        for (Dept sParent : depts) {
            DeptVo subjectTreeSubject = new DeptVo();
            BeanUtils.copyProperties(sParent, subjectTreeSubject);
            subjectTreeSubject.setChildren(findChildren(subjectTreeSubject,depts1));
            listTree.add(subjectTreeSubject);
        }
        return listTree;
    }

    /**
     *递归查询树结构
     *@author Hugo
     *@date 2021-04-19 14:27
     **/
    public List<DeptVo> findChildren(DeptVo subjectTreeSubject,List<Dept> sChildren){
        if (subjectTreeSubject == null) {
            return null;
        }
        if (sChildren.size() > 0) {
            ArrayList<DeptVo> treeChildrenList = new ArrayList<>();
            for (Dept sC : sChildren) {
                if (sC.getPid().equals(subjectTreeSubject.getId())) {
                    DeptVo treeSubject = new DeptVo();
                    BeanUtils.copyProperties(sC, treeSubject);
                    treeSubject.setChildren(findChildren(treeSubject,sChildren));
                    treeChildrenList.add(treeSubject);
                }
            }
            return treeChildrenList;
        } else {
            return null;
        }

    }

}
